Skip to contentMethod: removeAxioms(NamedResource, Map)
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
18: import cz.cvut.kbss.ontodriver.model.Assertion;
19: import cz.cvut.kbss.ontodriver.model.NamedResource;
20: import cz.cvut.kbss.ontodriver.model.Value;
21: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
22: import cz.cvut.kbss.ontodriver.owlapi.util.MutableRemoveAxiom;
23: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
24: import org.semanticweb.owlapi.model.*;
25: import org.semanticweb.owlapi.search.EntitySearcher;
26:
27: import java.util.*;
28: import java.util.stream.Collectors;
29:
30: class EpistemicAxiomRemover {
31:
32: private final OwlapiAdapter owlapiAdapter;
33: private final OWLOntology ontology;
34: private final OWLDataFactory dataFactory;
35: private final OntologySnapshot snapshot;
36:
37: EpistemicAxiomRemover(OwlapiAdapter adapter, OntologySnapshot snapshot) {
38: this.owlapiAdapter = adapter;
39: this.snapshot = snapshot;
40: this.ontology = snapshot.getOntology();
41: this.dataFactory = snapshot.getDataFactory();
42: }
43:
44: void remove(AxiomDescriptor descriptor) {
45: final List<OWLOntologyChange> changes = new ArrayList<>();
46: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(descriptor.getSubject(), dataFactory);
47: for (Assertion a : descriptor.getAssertions()) {
48: switch (a.getType()) {
49: case CLASS:
50: changes.addAll(removeClassAssertionAxioms(individual));
51: break;
52: case DATA_PROPERTY:
53: changes.addAll(removeDataPropertyAssertions(individual, a));
54: break;
55: case OBJECT_PROPERTY:
56: changes.addAll(removeObjectPropertyAssertions(individual, a));
57: break;
58: case ANNOTATION_PROPERTY:
59: changes.addAll(removeAnnotationAssertions(individual, a));
60: }
61: }
62: if (!changes.isEmpty()) {
63: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(changes));
64: }
65: }
66:
67: private Collection<OWLOntologyChange> removeClassAssertionAxioms(OWLNamedIndividual individual) {
68: final Collection<OWLClassExpression> types = EntitySearcher.getTypes(individual, ontology);
69: return types.stream().map(cls -> new MutableRemoveAxiom(ontology,
70: dataFactory.getOWLClassAssertionAxiom(cls, individual))).collect(Collectors.toList());
71: }
72:
73: private Collection<OWLOntologyChange> removeClassAssertionAxioms(OWLNamedIndividual individual,
74: Set<Value<?>> values) {
75: return values.stream().map(value -> {
76: final OWLClass owlClass = dataFactory.getOWLClass(IRI.create(value.stringValue()));
77: return new MutableRemoveAxiom(ontology, dataFactory.getOWLClassAssertionAxiom(owlClass, individual));
78: }).collect(Collectors.toList());
79: }
80:
81: private Collection<? extends OWLOntologyChange> removeDataPropertyAssertions(OWLNamedIndividual individual,
82: Assertion assertion) {
83: final OWLDataProperty dataProperty = dataFactory.getOWLDataProperty(IRI.create(assertion.getIdentifier()));
84: final Collection<OWLLiteral> values = EntitySearcher.getDataPropertyValues(individual, dataProperty, ontology);
85: return values.stream().map(value -> new MutableRemoveAxiom(ontology,
86: dataFactory.getOWLDataPropertyAssertionAxiom(dataProperty, individual, value)))
87: .collect(Collectors.toList());
88: }
89:
90: private Collection<? extends OWLOntologyChange> removeDataPropertyAssertions(OWLNamedIndividual individual,
91: Assertion assertion,
92: Set<Value<?>> values) {
93: final OWLDataProperty dataProperty = dataFactory.getOWLDataProperty(IRI.create(assertion.getIdentifier()));
94: return values.stream().map(value -> {
95: final OWLLiteral literal = OwlapiUtils
96: .createOWLLiteralFromValue(value.getValue(), dataFactory, owlapiAdapter.getLanguage());
97: return new MutableRemoveAxiom(ontology,
98: dataFactory.getOWLDataPropertyAssertionAxiom(dataProperty, individual, literal));
99: }).collect(Collectors.toList());
100: }
101:
102: private Collection<? extends OWLOntologyChange> removeObjectPropertyAssertions(OWLNamedIndividual individual,
103: Assertion assertion) {
104: final OWLObjectProperty objProperty = dataFactory.getOWLObjectProperty(IRI.create(assertion.getIdentifier()));
105: final Collection<OWLIndividual> values = EntitySearcher
106: .getObjectPropertyValues(individual, objProperty, ontology);
107: return values.stream().filter(OWLIndividual::isNamed).map(value -> new MutableRemoveAxiom(ontology,
108: dataFactory.getOWLObjectPropertyAssertionAxiom(objProperty, individual, value)))
109: .collect(Collectors.toList());
110: }
111:
112: private Collection<? extends OWLOntologyChange> removeObjectPropertyAssertions(OWLNamedIndividual individual,
113: Assertion assertion,
114: Set<Value<?>> values) {
115: final OWLObjectProperty objProperty = dataFactory.getOWLObjectProperty(IRI.create(assertion.getIdentifier()));
116: return values.stream().map(value -> {
117: final OWLIndividual object = OwlapiUtils
118: .getIndividual(NamedResource.create(value.stringValue()), dataFactory);
119: return new MutableRemoveAxiom(ontology,
120: dataFactory.getOWLObjectPropertyAssertionAxiom(objProperty, individual, object));
121: }).collect(Collectors.toList());
122: }
123:
124: private Collection<? extends OWLOntologyChange> removeAnnotationAssertions(OWLNamedIndividual individual,
125: Assertion assertion) {
126: final OWLAnnotationProperty annProperty = dataFactory
127: .getOWLAnnotationProperty(IRI.create(assertion.getIdentifier()));
128: final Collection<OWLAnnotationAssertionAxiom> values = EntitySearcher
129: .getAnnotationAssertionAxioms(individual.getIRI(), ontology);
130: return values.stream().filter(axiom -> axiom.getProperty().equals(annProperty))
131: .map(value -> new MutableRemoveAxiom(ontology, value)).collect(Collectors.toList());
132: }
133:
134: private Collection<? extends OWLOntologyChange> removeAnnotationAssertions(OWLNamedIndividual individual,
135: Assertion assertion,
136: Set<Value<?>> values) {
137: final OWLAnnotationProperty annProperty = dataFactory
138: .getOWLAnnotationProperty(IRI.create(assertion.getIdentifier()));
139: return values.stream().map(value -> {
140: OWLAnnotationValue av = null;
141: try {
142: av = IRI.create(value.stringValue());
143: } catch (IllegalArgumentException e) {
144: av = OwlapiUtils.createOWLLiteralFromValue(value.getValue(), dataFactory, owlapiAdapter.getLanguage());
145: }
146: assert av != null;
147: return new MutableRemoveAxiom(ontology,
148: dataFactory.getOWLAnnotationAssertionAxiom(annProperty, individual.getIRI(), av));
149: }).collect(Collectors.toList());
150: }
151:
152: void removeAxioms(NamedResource subject, Map<Assertion, Set<Value<?>>> toRemove) {
153: final List<OWLOntologyChange> changes = new ArrayList<>();
154: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
155:• for (Map.Entry<Assertion, Set<Value<?>>> e : toRemove.entrySet()) {
156: final IRI assertionIri = IRI.create(e.getKey().getIdentifier());
157:• if (ontology.containsDataPropertyInSignature(assertionIri)) {
158: changes.addAll(removeDataPropertyAssertions(individual, e.getKey(), e.getValue()));
159:• } else if (ontology.containsObjectPropertyInSignature(assertionIri)) {
160: changes.addAll(removeObjectPropertyAssertions(individual, e.getKey(), e.getValue()));
161:• } else if (ontology.containsAnnotationPropertyInSignature(assertionIri)) {
162: changes.addAll(removeAnnotationAssertions(individual, e.getKey(), e.getValue()));
163:• } else if (e.getKey().isClassAssertion()) {
164: changes.addAll(removeClassAssertionAxioms(individual, e.getValue()));
165: } else {
166: throw new IllegalArgumentException("Unknown assertion " + e.getKey());
167: }
168: }
169: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(changes));
170: }
171: }